package com.zhuoyue.system.service.impl;

import com.zhuoyue.common.constant.Constants;
import com.zhuoyue.common.core.domain.entity.TblProductBrand;
import com.zhuoyue.common.core.domain.entity.TblProductCategory;
import com.zhuoyue.common.core.domain.entity.TblProductClassify;
import com.zhuoyue.common.core.domain.entity.TblProductSeries;
import com.zhuoyue.common.exception.CustomException;
import com.zhuoyue.system.domain.TblProductInfo;
import com.zhuoyue.system.mapper.SysProductManageMapper;
import com.zhuoyue.system.mapper.TblDeviceZYCategoryMapper;
import com.zhuoyue.system.service.ISysProductManageService;
import com.zhuoyue.system.service.TblDeviceZYCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author zhanghui
 * @Date: 2021/08/04/9:08
 * @Description:
 */
@Service
public class SysProductManageServiceImpl implements ISysProductManageService {
    @Autowired
    SysProductManageMapper productManageMapper;

    @Override
    public List<TblProductBrand> selectBrandList(TblProductBrand brand) {
        return productManageMapper.selectBrandList(brand);
    }

    @Override
    public TblProductBrand selectBrandById(Long id) {
        return productManageMapper.selectBrandById(id);
    }

    @Override
    public List<TblProductBrand> selectBrandListForApp(TblProductBrand brand) {
        return productManageMapper.selectBrandListForApp(brand);
    }

    @Override
    public String checkBrandNameUnique(String brandName) {
        int count = productManageMapper.checkBrandNameUnique(brandName);
        return count > 0 ? Constants.NOT_UNIQUE : Constants.UNIQUE;
    }

    @Override
    public int insertBrand(TblProductBrand brand) {
        return productManageMapper.insertBrand(brand);
    }

    @Override
    public int updateBrand(TblProductBrand brand) {
        return productManageMapper.updateBrand(brand);
    }

    @Override
    public int deleteBrandByIds(Long[] brandIds) {
        return productManageMapper.deleteBrandByIds(brandIds);
    }

    @Override
    public List<TblProductClassify> selectClassifyList(TblProductClassify classify) {
        return productManageMapper.selectClassifyList(classify);
    }

    @Override
    public List<TblProductClassify> selectClassifyListForApp(TblProductClassify classify) {
        return productManageMapper.selectClassifyListForApp(classify);
    }

    @Override
    public TblProductClassify selectClassifyById(Long classifyId) {
        return productManageMapper.selectClassifyById(classifyId);
    }

    @Override
    public int selectClassifyChildrenCountByParentId(Long parentClassifyId) {
        return productManageMapper.selectClassifyChildrenCountByParentId(parentClassifyId);
    }

    @Override
    public int insertClassify(TblProductClassify classify) {
        // 若新增的是顶层的节点
        if (classify.getParentId() == 0) {
            classify.setAncestors("0");
        } else {
            TblProductClassify parentClassify = productManageMapper.selectClassifyById(classify.getParentId());
            // 若父节点不是正常状态, 则不允许新增子节点
            if (!Constants.STATUS_NORMAL.equals(parentClassify.getStatus())) {
                throw new CustomException("该产品分类已停用，不允许新增");
            }
            classify.setAncestors(parentClassify.getAncestors() + "," + classify.getParentId());
        }

        return productManageMapper.insertClassify(classify);
    }

    @Override
    public int updateClassify(TblProductClassify classify) {
        if (classify.getParentId() == 0) {
            classify.setAncestors("0");
        } else {
            TblProductClassify parentClassify = productManageMapper.selectClassifyById(classify.getParentId());
            TblProductClassify oldClassifyInfo = productManageMapper.selectClassifyById(classify.getId());
            String newAncestors = parentClassify.getAncestors() + "," + parentClassify.getId();
            String oldAncestors = oldClassifyInfo.getAncestors();

            // 若节点有改动
            if (!newAncestors.equals(oldAncestors)) {
                // 为自己设置祖先节点
                classify.setAncestors(newAncestors);
                // 为自己的子节点重新设置祖先节点
                updateChildrenClassifyAncestors(classify.getId(), newAncestors, oldAncestors);
            }
        }
        return productManageMapper.updateClassify(classify);
    }

    /**
     * 更新子节点的ancestors(因为修改节点的父节点, 也需要修应该其子节点的ancestor)
     *
     * @param classifyId
     * @param newAncestors 新的变更的部分
     * @param oldAncestors 旧的变更的部分
     */
    private void updateChildrenClassifyAncestors(Long classifyId, String newAncestors, String oldAncestors) {
        List<TblProductClassify> childrenList = productManageMapper.selectChildrenClassifyById(classifyId);
        if (childrenList.size() > 0) {
            for (TblProductClassify children : childrenList) {
                children.setAncestors(children.getAncestors().replace(oldAncestors, newAncestors));
            }
            productManageMapper.updateClassifyAncestorsList(childrenList);
        }
    }

    @Override
    public int deleteClassifyByIds(Long[] classifyIds) {
        return productManageMapper.deleteClassify(classifyIds);
    }

    @Override
    public List<TblProductSeries> selectSeriesList(TblProductSeries series) {
        return productManageMapper.selectSeriesList(series);
    }

    @Override
    public List<TblProductSeries> selectSeriesListForApp(TblProductSeries series) {
        return productManageMapper.selectSeriesListForApp(series);
    }

    @Override
    public TblProductSeries selectSeriesById(Long seriesId) {
        return productManageMapper.selectSeriesById(seriesId);
    }

    @Override
    public List<TblProductSeries> selectSeriesByClassifyId(Long classifyId) {
        return productManageMapper.selectSeriesListByClassifyId(classifyId);
    }

    @Override
    public int insertSeries(TblProductSeries series) {
        return productManageMapper.insertSeries(series);
    }

    @Override
    public int updateSeries(TblProductSeries series) {
        return productManageMapper.updateSeries(series);
    }

    @Override
    public int deleteSeriesByIds(Long[] seriesIds) {
        return productManageMapper.deleteSeries(seriesIds);
    }

    @Override
    public int selectSeriesCountByClassifyId(Long classifyId) {
        return productManageMapper.getSeriesCountByClassifyId(classifyId);
    }


    @Autowired
    private TblDeviceZYCategoryMapper categoryMapper;
    @Autowired
    private TblDeviceZYCategoryService deviceZYCategoryService;

    @Override
    public Boolean releaseData() {
        // 先更新tbl_product_info的product_type字段
        productManageMapper.updateProductType();

        // 依次新增五张表, 先查询需要新增的ID集合, 若集合元素数量大于0, 则批量插入数据到app表
        List<Long> needInsertCategoryId = productManageMapper.getNeedInsertDeviceCategoryId();
        if (needInsertCategoryId.size() > 0) {
            productManageMapper.insertDeviceProductApp(needInsertCategoryId);
        }

        List<Long> needInsertBrandId = productManageMapper.getNeedInsertProductBrandId();
        if (needInsertBrandId.size() > 0) {
            productManageMapper.insertProductBrandApp(needInsertBrandId);
        }

        List<Long> needInsertClassifyId = productManageMapper.getNeedInsertProductClassifyId();
        if (needInsertClassifyId.size() > 0) {
            productManageMapper.insertProductClassifyApp(needInsertClassifyId);
        }

        List<Long> needInsertSeriesId = productManageMapper.getNeedInsertProductSeriesId();
        if (needInsertSeriesId.size() > 0) {
            productManageMapper.insertProductSeriesApp(needInsertSeriesId);
        }

        List<String> needInsertInfoId = productManageMapper.getNeedInsertProductInfoId();
        if (needInsertInfoId.size() > 0) {
            productManageMapper.insertProductInfoApp(needInsertInfoId);
        }

        // 依次修改五张表, 先查询需要修改的ID集合, 若集合元素数量大于0, 在通过ID集合查询数据集合, 在将数据集合批量修改至app表
        List<Long> needUpdateCategoryIdList = productManageMapper.getNeedUpdateDeviceCategoryId();
        if (needUpdateCategoryIdList.size() > 0) {
            List<TblProductCategory> productCategoryList = productManageMapper.getDeviceCategoryByIds(needUpdateCategoryIdList);
            for (TblProductCategory tblProductCategory : productCategoryList) {
                categoryMapper.updateCategoryApp(tblProductCategory);
            }
        }

        List<Long> needUpdateBrandIdList = productManageMapper.getNeedUpdateProductBrandId();
        if (needUpdateBrandIdList.size() > 0) {
            List<TblProductBrand> productBrandList = productManageMapper.getProductBrandByIds(needUpdateBrandIdList);
            productManageMapper.updateProductBrandApp(productBrandList);
        }

        List<Long> needUpdateClassifyIdList = productManageMapper.getNeedUpdateProductClassifyId();
        if (needUpdateClassifyIdList.size() > 0) {
            List<TblProductClassify> productClassifyList = productManageMapper.getProductClassifyByIds(needUpdateClassifyIdList);
            productManageMapper.updateProductClassifyApp(productClassifyList);
        }

        List<Long> needUpdateSeriesIdList = productManageMapper.getNeedUpdateProductSeriesId();
        if (needUpdateSeriesIdList.size() > 0) {
            List<TblProductSeries> productSeriesList = productManageMapper.getProductSeriesByIds(needUpdateSeriesIdList);
            productManageMapper.updateProductSeriesApp(productSeriesList);
        }

        List<String> needUpdateInfoIdList = productManageMapper.getNeedUpdateProductInfoId();
        if (needUpdateInfoIdList.size() > 0) {
            List<TblProductInfo> productInfoList = productManageMapper.getProductInfoByIds(needUpdateInfoIdList);
            productManageMapper.updateProductInfoApp(productInfoList);
        }


//        List<String> tableNames = new ArrayList<>();
//        tableNames.add("device_zy_category");
//        tableNames.add("tbl_product_brand");
//        tableNames.add("tbl_product_classify");
//        tableNames.add("tbl_product_series");
//        tableNames.add("tbl_product_info");
//
//        // 截断表
//        for (String tableName : tableNames) {
//            productManageMapper.truncateTables(tableName + "_app");
//        }
//
//        productManageMapper.updateProductType();
//
//        // 复制表
//        for (String tableName : tableNames) {
//            productManageMapper.copyTablesData(tableName, tableName + "_app");
//        }
        return true;
    }

}
